package by.myteam.gonki.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.Timer;

import by.myteam.gonki.logic.GameLogic;
import by.myteam.gonki.logic.GameLogicEnemy;
import by.myteam.gonki.states.mainstate.EGameStates;
import by.myteam.gonki.states.mainstate.IGameState;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	public static MainFrame INSTANCE = new MainFrame();
	private IGameState currentState = EGameStates.MENU.getState();
	
	private Timer timer = new Timer(30, new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			if(currentState == EGameStates.PLAY.getState()){
				GameLogic.INSTANCE.move();
				GameLogicEnemy.INSTANCE.move();
				GameLogic.INSTANCE.checkOnCollision();
				GameLogicEnemy.INSTANCE.checkOnDeletingEnemy();
			}
			currentState.getView().repaint();
		}
	});
	
	private Timer travelTime = new Timer(1000, new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			GameLogic.INSTANCE.setTravelTime();
			GameLogicEnemy.INSTANCE.checkOnCreatingEnemy();

		}
	});

	private MainFrame() {
		this.add(currentState.getView());
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				currentState.keyPressed(e);
			}
		});
		getTimer().start();
	}
	
	public void changeState(IGameState currentState){
		this.remove(this.currentState.getView());
		this.currentState = currentState;
		this.add(this.currentState.getView());
		this.validate();
	}

	public Timer getTimer() {
		return timer;
	}

	public void setTimer(Timer timer) {
		this.timer = timer;
	}

	public Timer getTravelTime() {
		return travelTime;
	}

	public void setTravelTime(Timer travelTime) {
		this.travelTime = travelTime;
	}
	
	public IGameState getCurrentState(){
		return currentState;
	}

	/*public void startGame(){
		this.remove(state.getView());
		state = GameStates.GAME;
		
		travelTime.start();
		this.addKeyListener(new KeyStates());
		
	}

	public void GameOver() {
		timer.stop();
		this.removeKeyListener(null);
		this.state = GameStates.GAMEOVER;
		travelTime.stop();
	}

	public void PausePls() {
		this.remove(state.getView());
		state = GameStates.MENU;
		this.add(state.getView());
		this.validate();
		travelTime.stop();
	}
	
	public void SelectCar(){
		this.remove(state.getView());
		state = GameStates.SELECT_CAR;
		this.add(state.getView());
		validate();
	}*/
}
